package com.gjun.struts.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

import com.gjun.struts.bean.Gjunshops;
import com.gjun.struts.bean.GjunsystemPrivilege;
import com.gjun.struts.resource.GjunShopDAOIf;
import com.gjun.struts.resource.GjunShopResourceIf;

public class LoadStoresAction {
	private String queryType;

	private String curPage;

	private String province;

	private String city;

	private String status;

	private String startTimer;

	private String endTimer;

	private String status_label;

	private String condition;

	private String value;

	private String label;

	private String button;

	private String result;

	private GjunShopResourceIf shopDAO;

	public String getButton() {
		return button;
	}

	public void setButton(String button) {
		this.button = button;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public GjunShopResourceIf getShopDAO() {
		return shopDAO;
	}

	public void setShopDAO(GjunShopResourceIf shopDAO) {
		this.shopDAO = shopDAO;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getEndTimer() {
		return endTimer;
	}

	public void setEndTimer(String endTimer) {
		this.endTimer = endTimer;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getStartTimer() {
		return startTimer;
	}

	public void setStartTimer(String startTimer) {
		this.startTimer = startTimer;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatus_label() {
		return status_label;
	}

	public void setStatus_label(String status_label) {
		this.status_label = status_label;
	}

	public String getCurPage() {
		return curPage;
	}

	public void setCurPage(String curPage) {
		this.curPage = curPage;
	}

	public String getQueryType() {
		return queryType;
	}

	public void setQueryType(String queryType) {
		this.queryType = queryType;
	}

	public boolean checkLevel(String href, String label,
			List<GjunsystemPrivilege> accountPris) {
		boolean isHave = false;
		for (GjunsystemPrivilege g : accountPris) {

			if (g.getHref().equals(href) && g.getLabel().equals(label)) {
				isHave = true;
				System.out.println("ok===================");
				break;
			}
		}
		return isHave;
	}

	public String checkType1() {

		// 确认页码

		int cPage = 0;
		if (curPage == null) {
			cPage = 1;
		} else {
			try {
				cPage = Integer.parseInt(curPage);
			} catch (Exception ex) {
				cPage = 1;
			}
		}

		int pageSize = 2;

		int count = 0;

		int totalPage = 0;

		int start = 0;

		List<Gjunshops> shops = null;
		// System.out.println("bbbbbbbbbbbbbbbbbbb");

		try {
			count = shopDAO.getCount();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		totalPage = (count + pageSize - 1) / pageSize;

		if (cPage < 1) {
			cPage = 1;
		}

		if (cPage > totalPage) {
			cPage = totalPage;
		}
		start = (cPage - 1) * pageSize;
		try {
			shops = shopDAO.loadSomeShops(start, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JSONArray object = JSONArray.fromObject(shops);
		result = object.toString() + "#queryResult#" + cPage + "%1" 
				+ "%" + totalPage + "%" + count + "%" + toGBK(province) + "%"
				+ toGBK(city) + "%" + toGBK(status) + "%" + startTimer + "%"
				+ endTimer + "%" + toGBK(status_label) + "%" + toGBK(condition)
				+ "%" + toGBK(value) + "%" + toGBK(label);

		return "success";

	}
	
	
	public String checkType2(){
		int cPage = 0;
		if (curPage == null) {
			cPage = 1;
		} else {
			try {
				cPage = Integer.parseInt(curPage);
			} catch (Exception ex) {
				cPage = 1;
			}
		}

		int pageSize = 2;

		int count = 0;

		int totalPage = 0;

		int start = 0;
		
		List<Gjunshops> shops = null;
		
		status_label = toGBK(status_label);

		try {
			count = shopDAO.getCount(toGBK(province), toGBK(city),
					toGBK(status), startTimer, endTimer);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		totalPage = (count + pageSize - 1) / pageSize;

		if (cPage < 1) {
			cPage = 1;
		}

		if (cPage > totalPage) {
			cPage = totalPage;
		}
		start = (cPage - 1) * pageSize;
		try {
			shops = shopDAO.loadSomeShops(toGBK(province), toGBK(city),
					toGBK(status), startTimer, endTimer, start, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JSONArray object = JSONArray.fromObject(shops);
		result = object.toString() + "#queryResult#" + cPage + "%2" 
				+ "%" + totalPage + "%" + count + "%" + toGBK(province) + "%"
				+ toGBK(city) + "%" + toGBK(status) + "%" + startTimer + "%"
				+ endTimer + "%" + toGBK(status_label) + "%" + toGBK(condition)
				+ "%" + toGBK(value) + "%" + toGBK(label);
		return "success";
	}
	
	
	public String checkType3(){
//		 确认页码

		int cPage = 0;
		if (curPage == null) {
			cPage = 1;
		} else {
			try {
				cPage = Integer.parseInt(curPage);
			} catch (Exception ex) {
				cPage = 1;
			}
		}

		int pageSize = 2;

		int count = 0;

		int totalPage = 0;

		int start = 0;

		List<Gjunshops> shops = null;
		
		try {
			count = shopDAO.getCount(condition, value);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		totalPage = (count + pageSize - 1) / pageSize;

		if (cPage < 1) {
			cPage = 1;
		}

		if (cPage > totalPage) {
			cPage = totalPage;
		}
		start = (cPage - 1) * pageSize;
		try {
			shops = shopDAO
					.loadSomeShops(condition, value, start, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JSONArray object = JSONArray.fromObject(shops);
		result = object.toString() + "#queryResult#" + cPage + "%3" 
				+ "%" + totalPage + "%" + count + "%" + toGBK(province) + "%"
				+ toGBK(city) + "%" + toGBK(status) + "%" + startTimer + "%"
				+ endTimer + "%" + toGBK(status_label) + "%" + toGBK(condition)
				+ "%" + toGBK(value) + "%" + toGBK(label);
		
		return "success";
	}

	/*
	public String execute() {

		String en = ServletActionContext.getRequest().getCharacterEncoding();
		System.out.println("en:  " + en);
		String checkType = "";

		// 确认页码

		int cPage = 0;
		if (curPage == null) {
			cPage = 1;
		} else {
			try {
				cPage = Integer.parseInt(curPage);
			} catch (Exception ex) {
				cPage = 1;
			}
		}

		int pageSize = 2;

		int count = 0;

		int totalPage = 0;

		int start = 0;

		List<Gjunshops> shops = null;
		// System.out.println("bbbbbbbbbbbbbbbbbbb");
		if (queryType == null || "1".equals(queryType)) {
			checkType = "1";
			try {
				count = shopDAO.getCount();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			totalPage = (count + pageSize - 1) / pageSize;

			if (cPage < 1) {
				cPage = 1;
			}

			if (cPage > totalPage) {
				cPage = totalPage;
			}
			start = (cPage - 1) * pageSize;
			try {
				shops = shopDAO.loadSomeShops(start, pageSize);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// String province = "";
		// String city = "";
		// String status = "";
		// String startTimer = "";
		// String endTimer = "";
		// String status_label = "";
		if ("2".equals(queryType)) {

			HttpSession session = ServletActionContext.getRequest()
					.getSession();
			HttpServletRequest req = ServletActionContext.getRequest();

			List<GjunsystemPrivilege> allPris = (List<GjunsystemPrivilege>) session
					.getAttribute("allPris");
			List<GjunsystemPrivilege> accountPris = (List<GjunsystemPrivilege>) session
					.getAttribute("accountPris");

			String clientURI = req.getRequestURI();

			for (GjunsystemPrivilege g : allPris) {

				if (clientURI.lastIndexOf(g.getHref()) != -1) {
					System.out.println("复杂需要验证====yy=uuu======= " + g.getHref()
							+ "    " + toGBK(button));
					boolean checkResult = checkLevel(g.getHref(),
							toGBK(button), accountPris);
					if (!checkResult) {
						result = "levelFaild";

						return "success";
					}
				}
			}

			checkType = "2";

			status_label = toGBK(status_label);

			try {
				count = shopDAO.getCount(toGBK(province), toGBK(city),
						toGBK(status), startTimer, endTimer);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			totalPage = (count + pageSize - 1) / pageSize;

			if (cPage < 1) {
				cPage = 1;
			}

			if (cPage > totalPage) {
				cPage = totalPage;
			}
			start = (cPage - 1) * pageSize;
			try {
				shops = shopDAO.loadSomeShops(toGBK(province), toGBK(city),
						toGBK(status), startTimer, endTimer, start, pageSize);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		// String condition = "";
		// String value = "";
		// String label = "";
		if ("3".equals(queryType)) {

			HttpSession session = ServletActionContext.getRequest()
					.getSession();
			HttpServletRequest req = ServletActionContext.getRequest();

			List<GjunsystemPrivilege> allPris = (List<GjunsystemPrivilege>) session
					.getAttribute("allPris");
			List<GjunsystemPrivilege> accountPris = (List<GjunsystemPrivilege>) session
					.getAttribute("accountPris");

			String clientURI = req.getRequestURI();

			for (GjunsystemPrivilege g : allPris) {

				if (clientURI.lastIndexOf(g.getHref()) != -1) {
					System.out.println("基本需要验证==ggg=====kk===== " + g.getHref()
							+ "    " + toGBK(button));
					boolean checkResult = checkLevel(g.getHref(),
							toGBK(button), accountPris);
					if (!checkResult) {
						result = "levelFaild";

						return "success";
					}
				}
			}

			System.out.println("执行了/////////////");

			checkType = "3";

			try {
				count = shopDAO.getCount(condition, value);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			totalPage = (count + pageSize - 1) / pageSize;

			if (cPage < 1) {
				cPage = 1;
			}

			if (cPage > totalPage) {
				cPage = totalPage;
			}
			start = (cPage - 1) * pageSize;
			try {
				shops = shopDAO
						.loadSomeShops(condition, value, start, pageSize);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		JSONArray object = JSONArray.fromObject(shops);
		result = object.toString() + "#queryResult#" + cPage + "%" + checkType
				+ "%" + totalPage + "%" + count + "%" + toGBK(province) + "%"
				+ toGBK(city) + "%" + toGBK(status) + "%" + startTimer + "%"
				+ endTimer + "%" + toGBK(status_label) + "%" + toGBK(condition)
				+ "%" + toGBK(value) + "%" + toGBK(label);
		// System.out.println(object.toString() + "#queryResult#" + cPage + "%"
		// + checkType
		// + "%" + totalPage + "%" + count + "%" + toGBK(province) + "%" +
		// toGBK(city)
		// + "%" + toGBK(status) + "%" + startTimer + "%" + endTimer + "%"
		// + toGBK(status_label) + "%" + toGBK(condition) + "%" + toGBK(value) +
		// "%" + toGBK(label));
		return "success";
	}
	
	*/

	public String toGBK(String str) {
		try {
			return new String(str.getBytes("iso-8859-1"), "gbk");
		} catch (Exception ex) {
			return str;
		}
	}
}
